//
//  ST_ConversionDetailViewController.swift
//  btvideo
//
//  Created by mac on 2020/10/12.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyUserDefaults
import SwiftyAttributes

class ST_ConversionDetailViewController: ST_BTLightBarViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
//        self.title_nav_btn.setTitleForAllStates("兑换记录")
        
        view.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.top.equalTo(own_navigation_bar.snp.bottom)
        }
        
        scrollView.addSubview(contentView)
        contentView.snp.makeConstraints { (make) in
            make.width.equalTo(ScreenWidth)
            make.height.equalTo(ScreenHeight)
            make.edges.equalToSuperview()
        }
        
        contentView.addSubview(icon_image_view)
        icon_image_view.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(icon_image_view.snp.width).multipliedBy(0.69)
        }
        
        contentView.addSubview(midView)
        midView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(icon_image_view.snp.bottom).offset(0)
            make.height.equalTo(74)
        }
        
        
        midView.addSubview(tian_key_label)
        tian_key_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(10)
            make.height.equalTo(22)
        }
        
        midView.addSubview(tian_value_label)
        tian_value_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalTo(tian_key_label.snp.bottom).offset(10)
            make.height.equalTo(22)
        }
        midView.addSubview(love_btn)
        love_btn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalToSuperview()
            make.height.equalTo(28)
            make.width.equalTo(68)
        }
        
        if Defaults[.isPanda]{
            return
        }
        
        contentView.addSubview(object_subscribe_label)
        object_subscribe_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalTo(midView.snp.bottom).offset(20)
            make.height.equalTo(22)
        }
        
        
        contentView.addSubview(tips_box_view)
        tips_box_view.snp.makeConstraints { (make) in
            make.right.left.equalToSuperview()
            make.top.equalTo(object_subscribe_label.snp.bottom).offset(10)
        }
        
        tips_box_view.addSubview(tips_label)
        tips_label.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 10, left: 15, bottom: 10, right: 15))
        }
        
        
        
    }

//    MARK: - Properties
    
    fileprivate lazy var scrollView:UIScrollView = {
       let control = UIScrollView()
        if #available(iOS 11.0, *) {
            control.contentInsetAdjustmentBehavior = .never
        }
        control.showsVerticalScrollIndicator = false
        control.showsHorizontalScrollIndicator = false
        
        return control
    }()
    
    fileprivate lazy var contentView:UIView = {
       let control = UIView()
        control.backgroundColor = UIColor.init(hex: 0xF5F8FA)
        return control
    }()
    
    
    
    fileprivate lazy var midView:UIView = {
       let control = UIView()
        control.backgroundColor = .white
        return control
    }()
    
    
    fileprivate lazy var tips_box_view:UIView = {
       let control = UIView()
        control.backgroundColor = .white
        return control
    }()
    
    fileprivate lazy var tips_label:UILabel = {
        
        let control = UILabel.init()
        
        control.numberOfLines = 0
        
        control.attributedText = SCComponets.rich_text_storage_component.json["g_detail"]["content"].stringValue .withAttributes([Attribute.textColor(UIColor(hexString: "#333333")!),Attribute.font(UIFont.systemFont(ofSize: 13, weight: UIFont.Weight.regular)),Attribute.paragraphStyle(NSParagraphStyle.create_line_spacing_paragraph_style(lineSpacing: 3))])
        
        return control
    }()
    
    fileprivate lazy var icon_image_view:UIImageView = {
       let control = UIImageView()
        control.contentMode = .scaleAspectFill
        return control
    }()
    
    fileprivate lazy var tian_key_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.medium)
        return control
    }()

    fileprivate lazy var tian_value_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor(hexString: "#FEC207")
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    fileprivate lazy var object_subscribe_label:UILabel = {
        let control = UILabel.init()
        control.text = "物品描述"
        control.textColor = UIColor(hexString: "#ACB4C0")
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.regular)
        return control
    }()
    
    fileprivate lazy var love_btn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = UIColor.init(hex: 0xFEC207)
        control.cornerRadius = 28/2.0
//        control.borderColor = UIColor(white: 0, alpha: 0.1)
//        control.borderWidth = 1
        control.setTitleForAllStates("兑换")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        control.setTitleColor(UIColor.white, for: UIControl.State.normal)
        control.addTarget(self, action: #selector(love_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
//    MARK: - data
    var data:ST_ConversionGoodsModel?{
        didSet{
            
            guard let d = data else { return  }
            
            icon_image_view.pin_setImage(from:d.image.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            tian_key_label.text = d.name
            
            tian_value_label.text = d.integral.string + " \(Defaults[.PandaStr])"
            
            self.title_nav_btn.setTitleForAllStates(d.name)
            
        }
    }
    
}


//MARK: - Event
extension ST_ConversionDetailViewController{
    
    @objc fileprivate func love_handle(){
        
        guard let d = data else { return  }
        
        let vc = ST_ConversionConfirmViewController(data: d)
        
        let nav = UINavigationController.create_hiding_bar_navigation_controller(rootViewController: vc)
        
        nav.overCurrentContext = true
        
        GlobalPresent(toController: nav)
        
    }
    
}
